package com.hdax.dm.controller;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.hdax.dm.beans.order.order;
import com.hdax.dm.constant.payConstant;
import com.hdax.dm.exceptions.DmException;
import com.hdax.dm.feign.payFeign;
import com.hdax.dm.utils.response.CommonResponse;
import com.hdax.dm.utils.response.ResponseUtil;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.Map;

//@RestController
@RequestMapping("/pay")
@RestControllerAdvice
public class PayController {

    @Autowired
    private payFeign payFeign;


//    @SneakyThrows//自动进行try catch
    @PostMapping(path = "zhifubaopay")
    /**/
    public void pay(@RequestBody Map<String,String> params, HttpServletResponse response) throws Exception{

        String orderNo = params.get("orderNo");
        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = orderNo;

        order o = payFeign.dingdan(orderNo);
        //输出
        if (o.getOrderType()==0){
            //获得初始化的AlipayClient
            AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipaydev.com/gateway.do",
                    payConstant.parameter.APPID,
                    payConstant.parameter.PRIVATEKEY,
                    "json",
                    "UTF-8",
                    payConstant.parameter.ALIPAYPUBLICKEY,
                    "RSA2");
            //设置请求参数
            AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
            //支付成功后的同步回调
            alipayRequest.setReturnUrl("http://localhost:8080/");
            //支付成功后的异步回调
            alipayRequest.setNotifyUrl("http://ar2suj.natappfree.cc/order/payed");


            //付款金额，必填
            String total_amount = String.valueOf(o.getTotalAmount());
            //订单名称，必填
            String subject = o.getItemName();
            //商品描述，可空
            String body = "大觅网订单";

            alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                    + "\"total_amount\":\""+ total_amount +"\","
                    + "\"subject\":\""+ subject +"\","
                    + "\"body\":\""+ body +"\","
                    + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

            //请求
            String result = alipayClient.pageExecute(alipayRequest).getBody();
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = response.getWriter();
            out.println(result);
            return;
        }else{
            throw new DmException("4567","该订单超时或以支付");
        }
    }

    //局部异常处理
    @ExceptionHandler(value = {DmException.class})
    public CommonResponse<String> exceptionHandler(DmException e){
        return ResponseUtil.returnFail(e.getErrorCode()+"",e.getMessage());
    }

}
